<template>
  <div>
    <!-- <div class="app-main-title">课程销售统计</div> -->
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @clear="clear"
      @handleQuery="getList(1)"
    >
      <template v-if="meta.indexOf('CourseTeacherCHSExport') > -1" #btns>
        <el-button
          type="primary"
          :loading="exportLoading"
          @click="exportExcel"
          >{{ translate("export") }}</el-button
        >
      </template>
    </QueryForm>
    <div v-if="tjList && tjList.length > 0" class="total_wrap">
      <div class="first item">
        <div class="key">{{ translate("shanxuan") }}</div>
        <div class="value">{{ translate("heji") }}</div>
      </div>
      <div class="item_list">
        <div v-for="(item, i) in tjList" :key="i" class="item">
          <div class="key">{{ item.payMethod }}</div>
          <div class="value">
            <!-- <span>{{translate("renminbi")}}</span> -->
            <span class="price">{{ item.money }}</span>
          </div>
        </div>
      </div>
      <!-- <el-tooltip effect="dark" placement="left">
        <div class="jesm"><img style="width: 15px;margin-right: 2px;" src="@/assets/icon/question.png" alt="">{{translate('jesm')}}</div>
        <div slot="content" v-html="smTip" />
      </el-tooltip> -->
    </div>
    <el-table
      ref="tableRef"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table table_tj"
    >
      <el-table-column
        prop="teacherName"
        align="center"
        :label="translate('jlxm')"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        prop="phone"
        align="center"
        :label="translate('phone')"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        prop="levelName"
        align="center"
        :label="translate('level')"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        prop="courseName"
        align="center"
        :label="translate('kcmc')"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        prop="classNum"
        align="center"
        :label="translate('yskss')"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        prop="totalStudentNum"
        align="center"
        :label="translate('ydxys')"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        prop="actualStudentNum"
        align="center"
        :label="translate('sdxys')"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        prop="attendanceRate"
        align="center"
        :label="translate('chuqinlv')"
        min-width="120"
        show-overflow-tooltip
      />
    </el-table>
    <Pagination
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import { teacherCHSV, teacherCHSVExport, venueList } from '@/api'
import { queryFormConfig } from './config'
import { parseTime, parseTimeb } from '@/utils/index'
import Pagination from '@/components/paginations'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      queryFormConfig,
      listLoading: false,
      exportLoading: false,
      tjList: [],
      list: [],
      pagination: {
        page: 1,
        rows: 10,
        firstQueryTime: new Date()
      },

      total: 0,
      filters: {
        listType: '1',
        time: [parseTimeb(30).slice(0, 10), parseTime(new Date()).slice(0, 10)]
      },
      meta: []
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
  },
  methods: {
    handleSizeChange(val) {
      this.pagination.rows = val
      this.getList(1)
    },
    // 获取场馆列表
    getVenueList() {
      venueList({
        param: { flag: '0' }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode !== '000000') return
          const arr = res.recordList
          this.queryFormConfig.formItems[1].options = arr
          this.getList(1)
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    getList(page) {
      this.listLoading = true
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (param.time && param.time.length > 0) {
        param.startDate = param.time[0]
        param.endDate = param.time[1]
      } else {
        param.startDate = ''
        param.endDate = ''
      }
      teacherCHSV({ param, pagination }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.list = res.recordList
          this.total = res.totalCount
          const tjList = res.data
          if (tjList) {
            let smTip = ''
            tjList.forEach(s => {
              smTip = smTip + s.tip + '<br />'
            })
            this.tjList = tjList
            this.smTip = smTip
          }
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    exportExcel() {
      this.exportLoading = true
      const param = { ...this.filters }
      if (param.time && param.time.length > 0) {
        param.startDate = param.time[0]
        param.endDate = param.time[1]
      } else {
        param.startDate = ''
        param.endDate = ''
      }
      teacherCHSVExport({
        param
      })
        .then((res) => {
          this.exportLoading = false
          if (res.errorCode === '000000') {
            window.open(res.record.showUrl, '_blank')
          }
        })
        .catch(() => { this.exportLoading = false })
    },
    // 清空筛选项
    clear() {
      this.filters = {
        time: this.filters.time,
        listType: '1'
      }
      this.getList()
    }
  }
}
</script>

